package com.example.servlet.ClassServlet;

import com.example.bean.Class;
import com.example.dao.ClassDao.ClassAddDao;
import com.example.dao.ClassDao.ClassAddDaoImp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AddClassServlet", value = "/classadd")
public class ClassAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.接收浏览器发来的信息
        request.setCharacterEncoding("utf-8");
        String cname = request.getParameter("cname");
        System.out.println("获取到的班级名称为:"+cname);
        String cgrade = request.getParameter("cgrade");
        System.out.println("获取到的年级为:"+cgrade);
        String ctname = request.getParameter("ctname");
        System.out.println("获取到的班主任名称为:"+ctname);
        String cflag = request.getParameter("cflag");
        System.out.println("获取到的班级口号为:"+cflag);
        String cnum = request.getParameter("cnum");
        System.out.println("获取到的班级人数为:"+cnum);


        request.setCharacterEncoding("utf-8");
        //2.将获取到的内容给Dao层进行处理
        Class cl=new Class(cname,cgrade,ctname,cflag,cnum);
        ClassAddDao classAddDao=new ClassAddDaoImp();
        response.setContentType("html/text;charset=utf-8");
        int res = classAddDao.getClassAdd(cl);
        if(1==res){
            request.getSession().setAttribute("res",res);
            System.out.println("创建班级成功!");
            request.getRequestDispatcher("classadd.jsp").forward(request,response);
        }

    }
}
